package myLucene;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Vector;

import org.apache.lucene.analysis.KeywordAnalyzer;
import org.apache.lucene.analysis.de.GermanAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.TermVectorMapper;
import org.apache.lucene.index.TermVectorOffsetInfo;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 *  
 *
 *
 *
 **/

public class Indexer {
	// Directory where the Index will be stored
	Directory indexDir = null;
	String indexDirPath = "";
	String dataPath = "";
	long start;

	/**
	 * 
	 * @param indexPath
	 *            Path to the Index Directory
	 * @param dataPath
	 *            Path to the Data to be indexed
	 * @throws Exception 
	 */
	public Indexer(String indexPath, String dataPath) throws Exception {
		System.out.println("Create Indexer");
		this.start = System.currentTimeMillis();
		this.indexDirPath = indexPath;
		this.dataPath = dataPath;
		this.indexDir = FSDirectory.open(new File(indexPath));
		this.indexDirectory(dataPath);

	}

	/**
	 * Index all file in the directory
	 * 
	 * @param path
	 *            : directory to be indexed
	 * @throws Exception 
	 */
	public void indexDirectory(String dataPath) throws Exception {
		final File dataDir = new File(dataPath);
		if (!dataDir.isDirectory() || !dataDir.canRead()) {
			System.out.println("Data directory does not exist: " + dataPath);
			System.exit(1);
		}
		/**
		 * Keyword or Standardanalyzer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		 */
		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(
				Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);

		// IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(
		// Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);

		writer.setRAMBufferSizeMB(2048);
		System.out.println("Writing Index to Directory: " + indexDirPath);
		System.out.println("Starting indexing");
		this.doIndexing(writer, new File(dataPath));

	}

	private Vector<String> sortDataFiles(String[] files) {
		Vector<String> v = new Vector<String>();
		for (String s : files) {
			v.add(s);
		}
		Collections.sort(v);
		return v;
	}

	private void doIndexing(IndexWriter writer, File file) throws Exception {
		if (file.canRead() && file.isDirectory()) {

			System.out.println("Read data files in directory: "
					+ file.getAbsolutePath());
			Document doc = new Document();//		

			Vector<String> v = this.sortDataFiles(file.list());
			for (String fileName : v) {
				System.out.println(fileName);
				String filePath = file.getAbsolutePath() + "/" + fileName;
				System.out.println("Index " + filePath);
				FileInputStream in = new FileInputStream(filePath);
				BufferedReader br = new BufferedReader(new InputStreamReader(
						in, "utf-8"));
				int counter = 0;
				int linums = 0;
				String line = br.readLine();
				while (line != null) {
					// System.out.println("LINE NUMBER: "+ linums + " "+line);
					linums++;
					if (line.isEmpty()) {
						if (counter == 1) {
							if (doc.getValues("index").length > 3) {
								DocumentIndex.DeWacDoc(line, doc);
								// String[] values = doc.getValues("lemma") ;
								// for (String va: values){
								// System.out.println("Values: "+va);
								// }
								writer.addDocument(doc);
								// System.out.println("Index doc with number "+writer.maxDoc());
							}
							counter = 0;

							doc = new Document();
						} else {
							counter++;
						}
					} else {
						DocumentIndex.DeWacDoc(line, doc);

					}
					line = br.readLine();
				}
				System.out.println("END OF FILE " + filePath);
				System.out.println("Finished indexing..." + GetTime.getTime());
				// writer.commit();
				// System.out.println("Commited..."+filePath);

			}

			long timeInd = System.currentTimeMillis();
			String time = GetTime.getDuration(start, timeInd);
			System.out.println("Time needed: " + time);
		}
		// System.out.println("Optimize!");
		// writer.optimize();
		// System.out.println("End optimizing...");
		System.out.println("Close Writer!");
		writer.close();
	}

	public static String getIndexPath(Indexer index) {
		return index.indexDirPath;
	}

	static Document getDocument(File f) throws IOException {
		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f)));
		doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		return doc;
	}

	public Directory getIndexDirectory() {
		return this.indexDir;
	}
}

// ////////////////////////////////////////////////////////////////////////////

